<?php
/*
Primary Author: Alex Arey
Secondary: Derek Shue
Documentation: Cheng Yi
Updated: 12/10/2011
Module Description: The Terms module adds/finds/updates/deletes Terms from the database.
*/
class Application_Model_DbTable_Terms extends Zend_Db_Table_Abstract
{
	protected $_name = 'TERMS';
	
	function getTerm($where){
	$rows = $this->fetchRow($where);
       	if (!$rows) {
          	 return null;
		}
      return $rows->toArray();
	}
	
	function getTerms($where){
        	$rows = $this->fetchAll($where);
        	if (!$rows) {
          	 return null;
		}
      return $rows->toArray();
	}
	function updateTerm($term, $year, $sStart, $sEnd, $fStart, $fEnd, $piStart, $piEnd, $pvStart, $pvEnd, $aStart, $aEnd){
		$data = array(
			'STUDENTAPPSTART'=> $sStart,
			'STUDENTAPPEND' => $sEnd,
			'FACULTYCOURSESTART' => $fStart,
			'FACULTYCOURSEEND' => $fEnd,
			'PARTNERINTERNSTART' => $piStart,
			'PARTNERINTERNEND' => $piEnd,
			'PARTNERVOLUNTEERSTART' => $pvStart,
			'PARTNERVOLUNTEEREND' => $pvEnd,
			'ADMINISTRATIVESTART' => $aStart,
			'ADMINISTRATIVEEND' => $aEnd,
        	);
        	$this->update($data, 'TERM = \''. $term . '\' and YEAR =  '. $year);
	}
	function addTerm($term, $year, $sStart, $sEnd, $fStart, $fEnd, $piStart, $piEnd, $pvStart, $pvEnd, $aStart, $aEnd){
		$data = array(
		    'TERM' => $term,
			'YEAR' => $year,
			'STUDENTAPPSTART'=> $sStart,
			'STUDENTAPPEND' => $sEnd,
			'FACULTYCOURSESTART' => $fStart,
			'FACULTYCOURSEEND' => $fEnd,
			'PARTNERINTERNSTART' => $piStart,
			'PARTNERINTERNEND' => $piEnd,
			'PARTNERVOLUNTEERSTART' => $pvStart,
			'PARTNERVOLUNTEEREND' => $pvEnd,
			'ADMINISTRATIVESTART' => $aStart,
			'ADMINISTRATIVEEND' => $aEnd,
        	);
        	$this->insert($data);
	}
	function deleteTerm($where){
		$this->delete($where);
	}
	
	function getTermsByDate($date, $type){
		if($type=='STU'){
			$colPrefix = 'STUDENTAPP';
		}
		else if($type == 'CPI'  ){
			$colPrefix = 'PARTNERINTERN';
		}
		else if($type == 'CPV'){
			$colPrefix = 'PARTNERVOLUNTEER';
		}
		else if($type == 'FAC'){
			$colPrefix = 'FACULTYCOURSE';
		}
		else if($type =='ADV' ||$type=='ADI'|| $type=='ADC'){
			$colPrefix = 'ADMINISTRATIVE';
		}
		$where = $colPrefix . 'START <= \'' . $date . '\' and ' . $colPrefix . 'END >= \'' . $date.'\'';
		
		$rows = $this->fetchAll($where);
		if (!$rows) {
			return null;
		}
		return $rows->toArray();
	}
}
